﻿using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace WebSite.ViewModels.Fields
{
    public class PasswordField : FieldBase
    {
        private readonly string m_Value;
        private readonly bool m_UseValidation;

        public PasswordField(string caption, string name, string value, bool useValidation = true)
            : base(caption, name, value)
        {
            m_Value = value;
            m_UseValidation = useValidation;
        }

        public override IHtmlString Render(HtmlHelper htmlHelper)
        {
            StringBuilder stringBuilder = new StringBuilder();
            TagBuilder spanTagBuilder = new TagBuilder("span");
            spanTagBuilder.Attributes["style"] = "width:100%";
            stringBuilder.Append(htmlHelper.Password(Name, m_Value));
            if (m_UseValidation)
            {
                stringBuilder.Append(htmlHelper.ValidationMessage(Name));
            }
            spanTagBuilder.InnerHtml = stringBuilder.ToString();
            return MvcHtmlString.Create(spanTagBuilder.ToString(TagRenderMode.Normal));
        }
    }
}